import { createRouter,createWebHashHistory,RouteRecordRaw } from "vue-router"
import { getToken,removeToken } from "../request/cookies"

const routes: Array<RouteRecordRaw> = [
    {
        path:'/capsual',
        name:'capsual',
        component:()=>import('../components/UserCapsual.vue'),
        meta:{
            title: '我的胶囊',
            needAuth: true,
        }
    },
    {
        path:'/activity',
        name:'activity',
        component:()=>import('../components/UserActivity.vue'),
        meta:{
            title: '我的活动',
            needAuth: true,
        }
    },
    {
        path:'/offiactivity',
        name: 'offiactivity',
        component:()=>import('../components/ActivityPage.vue'),
        meta:{
            title: '活动详情',
            needAuth: true,
        }
    },
    {
        path:'/content',
        name:'content',
        component:()=>import('../components/CapsualContent.vue'),
        meta:{
            title: '胶囊内容',
            needAuth: true,
        }
    },
    {
        path:'/activityControl',
        name:'activityControl',
        component:()=>import('../components/ActivityControl.vue'),
        meta:{
            title: '活动管理',
            needAuth: true,
        }
    },
    {
        path:'/report',
        name:'report',
        component:()=>import('../components/ReportControl.vue'),
        meta: {
            title: '举报管理',
            needAuth: true,
        }
    },
    {
        path:'/',
        name:'login',
        component:()=>import('../components/UserLogin.vue'),
    },
    {
        path:'/notice',
        name:'notice',
        component:()=>import('../components/UserNotice.vue'),
        meta: {
            title: '我的通知',
            needAuth: true,
        }
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../components/About.vue'),
        meta: {
            title: '关于时间胶囊',
            needAuth: true
        }
    }
]
const router = createRouter({
    history:createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    if(to.name == 'login') removeToken(); //初始化的时候
    let token = getToken() || null;
    if(to.name != 'login'){
        if(token == null){
            return next('/');  //回到登录页
        }else if(token != null){
            next();
        }
    }else next();
})
router.afterEach((to, from) => {
    document.title = to.meta.title as string || "时间胶囊";
})
export default router